package com.qywx.filters;

import com.alibaba.fastjson.JSONObject;
import com.qywx.service.impl.DepartmentServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 *
 */
public class CloudServerProtectInterceptor implements HandlerInterceptor {

    private final static Logger log = LoggerFactory.getLogger(DepartmentServiceImpl.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        // 从请求头中获取 Gateway Token
        //String token = request.getHeader(CloudConstant.GATEWAY_TOKEN_HEADER);
        //String gatewayToken = new String(Base64Utils.encode(CloudConstant.GATEWAY_TOKEN_VALUE.getBytes()));
        String requestURI = request.getRequestURI();
        System.out.println("--------------------->过滤器：请求地址"+requestURI);
        log.info("===========过滤器：请求地址===========" + requestURI, requestURI);
        if(requestURI.equals("/swagger-ui.html")||requestURI.equals("/configuration/ui")||requestURI.equals("/swagger-resources")||requestURI.equals("/v2/api-docs")||requestURI.equals("/configuration/security")){
            return true;
        }
        if(requestURI.equals("/photo/read")||requestURI.contains("export")){
            return true;
        }
        if(!requestURI.equals("/sys/login")){ //登陆
            ServletContext servletContext = request.getServletContext();
            List tokens = (List) servletContext.getAttribute("tokens");
            String token = request.getHeader("token");
            if(StringUtils.isEmpty(token) || !tokens.contains(token)){
                log.info("=========tokens=========="+tokens,tokens);
                log.info("=========token=========="+token,token);
                //返回错误信息
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                //创建json对象
                JSONObject res = new JSONObject();
                res.put("status",HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                res.put("data","用户未登录");
                PrintWriter out = null ;
                out = response.getWriter();
                out.write(res.toString());
                out.flush();
                out.close();
                return false;
            }else{
                return true;
            }
        }
        return true;
        // 校验 Gateway Token的正确性
       /* String token = request.getHeader("token");
        if (!requestURI.equals("/sys/login")) {
            return true;
        } else {
            CloudResponse cloudResponse = new CloudResponse();
            CommonUtil.makeResponse(response,MediaType.APPLICATION_JSON_VALUE,
                    HttpServletResponse.SC_FORBIDDEN, cloudResponse.message("请通过网关获取资源"));
            return false;
        }*/
    }
}
